package org.yn.mkt.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;
import org.yn.base.util.*;
import org.yn.emp.domain.Employee;
import org.yn.mkt.dto.ClueBatchDTO;
import org.yn.mkt.dto.TransferBusinessDTO;
import org.yn.mkt.service.IClueService;
import org.yn.mkt.domain.Clue;
import org.yn.mkt.query.ClueQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.mkt.vo.ClueResourceVO;
import org.yn.mkt.vo.ClueVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@PermissionAnnotation(name="clue管理",descs = "clue管理")
@Api("clue管理接口")
@RestController
@RequestMapping("/clue")
public class ClueController {
    @Autowired
    public IClueService clueService;
//    private Object Clue;


    /**
     * 保存和修改公用的
     * @param clue  传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name="保存和修改Clue",descs = "保存和修改Clue")
    @ApiOperation(value = "保存和修改")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody Clue clue){
        try {
            if( clue.getId()!=null)
                clueService.update(clue);
            else
                clueService.insert(clue);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name="删除Clue",descs = "删除Clue")
    @ApiOperation(value = "删除")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            clueService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @PermissionAnnotation(name="获取单个Clue",descs = "获取单个Clue")
    @ApiOperation(value = "获取单个Clue")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id)
    {
        try {
            Clue clue = clueService.selectById(id);
            return AjaxResult.success(clue);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的XX信息
    * @return
    */
    @PermissionAnnotation(name="获取所有Clue",descs = "获取所有Clue")
    @ApiOperation(value = "获取所有Clue")
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Clue> list = clueService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name="分页查询Clue",descs = "分页查询Clue")
    @ApiOperation(value = "分页查询Clue")
    @PutMapping
    public AjaxResult queryPage(@RequestBody ClueQuery query)
    {
        try {
            PageInfo<Clue> pageInfo = clueService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }






    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量删除Clue",descs = "批量删除Clue")
    @ApiOperation(value = "批量删除Clue")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                clueService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败"+e.getMessage());
        }
    }

    /**
     * 保存分配信息
     */
        @PostMapping("/assign")
        public AjaxResult handleAssign(@RequestBody Clue clue, HttpServletRequest request){
            try {
                Employee loginUser = LoginContext.getLoginUser(request);
                clueService.handleAssign(clue,loginUser);

                return AjaxResult.success();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error("保存对象失败！"+e.getMessage());
            }
        }

    /**
     * 批量分配信息
     * @param clue
     * @param request
     * @return
     */
    @PostMapping("/batchAssign")
    public AjaxResult batchAssign(@RequestBody ClueBatchDTO clue, HttpServletRequest request){
        try {
            Employee loginUser = LoginContext.getLoginUser(request);
            clueService.batchAssign(clue,loginUser);

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 保存转商机
     * @param transferBusinessDTO
     * @param request
     * @return
     */
    @PostMapping("/transferBusiness")
    public AjaxResult transferBusiness(@RequestBody TransferBusinessDTO transferBusinessDTO, HttpServletRequest request){
        try {
            Employee loginUser = LoginContext.getLoginUser(request);
            clueService.transferBusiness(transferBusinessDTO,loginUser);

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file){
        try {
            System.out.println("____");
            System.out.println(file.isEmpty());
            System.out.println(file.getName());//file
            System.out.println(file.getOriginalFilename());//test11.xlsx
            System.out.println(file.getResource());//MultipartFile resource [file]
            InputStream inputStream = file.getInputStream();

            List<ClueVO> clueList = EasyExcelUtil.readExcel(inputStream, ClueVO.class);
            System.out.println("clueList____--");
            clueList.forEach(System.out::println);
            clueService.batchInsert(clueList);

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/dowmloadExcel")
    public void dowmloadExcel(HttpServletResponse response) throws IOException {
        System.out.println("-------");
        List<Clue> clueList = clueService.selectAll();
        clueList.forEach(System.out::println);

        EasyExcelUtils.writeWeb("clueOut",Clue.class,clueList,"clue1",response);
    }


    @PostMapping("/dowmloadExcel/quey")
    public void queyDowmloadExcel(HttpServletResponse response,@RequestBody ClueQuery query)
    {
        try {
           List<Clue> clueList= clueService.queyDowmloadExcel(query);
            System.out.println("clueList----");
            clueList.forEach(System.out::println);

            EasyExcelUtils.writeWeb("clueOut",Clue.class,clueList,"clue1",response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @GetMapping("/getClueResource")
    public AjaxResult getClueResource()   {
        try {
            List<ClueResourceVO> clueResourceVOList= clueService.getClueResource();

            return AjaxResult.success(clueResourceVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }


}
